// use std::collections::HashMap;

use macroquad::prelude::*;
use structopt::StructOpt;
use woolvortex::client::request_posts;
use woolvortex::feed::Feed;
use woolvortex::post::Post;
use woolvortex::user::UserCache;

/// View public posts tagged with <tag>
#[derive(Debug, StructOpt)]
struct Opt {
    /// A tag to view, without # (because bash)
    #[structopt()]
    tag: String,
}
/// Pick a higher resolution (default is 480p to not strain the network)

#[macroquad::main("Wool Vortex")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let options = Opt::from_args();
    let font = load_ttf_font_from_bytes(include_bytes!("../fonts/now-fl/Now-SemiBold.otf"))?;
    let apiposts = request_posts(&options.tag)?.posts();
    let users = UserCache::from_apiposts(&apiposts);

    let posts: Vec<Post> = apiposts
        .into_iter()
        .map(|post| Post {
            content: post.content,
            user_finger: post.account.acct,
        })
        .collect();

    let mut feed = Feed::new(&posts, &users);

    const SPEED: f32 = 10.0;

    loop {
        clear_background(DARKGRAY);

        if is_key_down(KeyCode::J) || is_key_down(KeyCode::Down) {
            feed.set_offset(feed.offset() - SPEED);
        }
        if is_key_down(KeyCode::K) || is_key_down(KeyCode::Up) {
            feed.set_offset(feed.offset() + SPEED);
        }
        feed.draw_feed(font);
        next_frame().await;
    }
    // test_request().await?;
    // Ok(())
}
